package com.wxzz.elearing.comment.service.impl;

import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.comment.dto.CurriculumCommentDto;
import com.wxzz.elearing.comment.dto.ImproveCommentDto;
import com.wxzz.elearing.comment.entity.CurriculumComment;
import com.wxzz.elearing.comment.entity.ImproveComment;
import com.wxzz.elearing.comment.service.ICurriculumCommentService;
import com.wxzz.elearing.comment.vo.CurriculumStarVo;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CurriculumCommentServiceImpl implements ICurriculumCommentService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ReturnVO saveComment(CurriculumComment comment) {
//        Skill skillInfo = skillMapper.selectById(comment.getSkillId());
//        if (skillInfo==null)
//            throw new ServiceException("请选择正确的技能");
        Query query = new Query();
        query.addCriteria(Criteria.where("curriculumId").is(comment.getCurriculumId()));
        query.addCriteria(Criteria.where("wxUserId").is(comment.getWxUserId()));
        List<CurriculumComment> curriculumComments = mongoTemplate.find(query, CurriculumComment.class);
        Integer starNum = 0;
        for (CurriculumComment curriculumComment : curriculumComments) {
            if (curriculumComment.getStar() != null && curriculumComment.getStar() != 0) {
                starNum = starNum + 1;
            }
        }
        if (starNum >= 1 && comment.getStar() != null && comment.getStar() > 0) {
            throw new ServiceException("您已参与过打星,无法再次打星了哦~");
        }
        comment.setCreateTime(LocalDateTime.now());
        CurriculumComment insert = mongoTemplate.insert(comment);
        // 增加 评论数量
//        Skill saveSkill = new Skill();
//        saveSkill.setId( skillInfo.getId() );
//        saveSkill.setCommentNum( skillInfo.getCommentNum()+1 );
//        skillMapper.updateById( saveSkill );

        return new ReturnVO("评论成功", insert.getUid());
    }

    @Override
    public ReturnVO<JgPageVo<List<CurriculumComment>>> listComment(CurriculumCommentDto curriculumCommentDto) {
        List<CurriculumComment> list = new ArrayList();
        Query query1 = new Query();
        String ImproveId = String.valueOf(curriculumCommentDto.getCurriculumId());
        CurriculumComment comment = mongoTemplate.findById(ImproveId, CurriculumComment.class);
        query1.with(new Sort(Sort.Direction.DESC, "createTime")).with(new PageRequest(curriculumCommentDto.getCurPage() - 1, curriculumCommentDto.getMaxPage()));
        query1.addCriteria(Criteria.where("curriculumId").is(curriculumCommentDto.getCurriculumId())).addCriteria(Criteria.where("_id").ne(ImproveId));
        Query queryCount = new Query();
        queryCount.addCriteria(Criteria.where("curriculumId").is(curriculumCommentDto.getCurriculumId()));
        long count = mongoTemplate.count(queryCount, "curriculumCommentCollection");
        list = mongoTemplate.find(query1, CurriculumComment.class);
//        if (curriculumCommentDto.getCurPage() == 1) {
//            list.add(0, comment);
//            if (list.size() != 1 && list.size() > 9) {
//                list.remove(list.size() - 1);
//            }
//        }
        JgPageVo jgPageVo = new JgPageVo<>(new PageInfo(list));
        jgPageVo.setCurIndex(curriculumCommentDto.getCurPage());
        jgPageVo.setPageSize(curriculumCommentDto.getMaxPage());
        jgPageVo.setPagesCount((int) Math.ceil(count * 1.0 / curriculumCommentDto.getMaxPage()));
        jgPageVo.setData(list);
        jgPageVo.setCount(count);
        return new ReturnVO("评论查询成功", jgPageVo);
    }

    public ReturnVO<CurriculumStarVo> getstarByCurriculumId(CurriculumCommentDto improveCommentDto) {
        Long curriculumId = improveCommentDto.getCurriculumId();
        Query query = new Query();
        query.addCriteria(Criteria.where("curriculumId").is(curriculumId));
        List<CurriculumComment> curriculumComments = mongoTemplate.find(query, CurriculumComment.class);
        int personCount = 0;
        int starCount = 0;
        for (int i = 0; i < curriculumComments.size(); i++) {
            Integer star = curriculumComments.get(i).getStar();
            if (star != null && star != 0) {
                personCount++;
                starCount += star;
            }
        }
        CurriculumStarVo curriculumStarVo = new CurriculumStarVo();
        if (personCount == 0) {
            curriculumStarVo.setStar(0.0F);
            return new ReturnVO("星级查询成功~", curriculumStarVo);
        }
        Double lv = starCount * 1.0 / personCount;
        BigDecimal b = new BigDecimal(lv);
        float v = b.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
        curriculumStarVo.setStar(v);
        return new ReturnVO("星级查询成功~", curriculumStarVo);
    }
}
